/**
* Dialog utility class
*/
jsx3.Class.defineClass("ux.core.util.Dialog",
        jsx3.lang.Object,
        null,
        function(commondialog) {


            commondialog.loadDialog = function(domNode, srcObject, strURL, isAsynchrone) {
                if (srcObject.dialog) {
                    srcObject.dialog.doToggleState(jsx3.gui.Dialog.MAXIMIZED);
                }
                else {
                    srcObject.dialog = ux.core.util.DynamicLoad.loadComponent(domNode, strURL, false, isAsynchrone);
                    srcObject.dialog.backReference = srcObject;
                }
                return srcObject.dialog;
            }

            /**
            * Resizes a dialog to fill it's parent's space, with a 10 pixel margin on each side of the dialog
            * (top, bottom, left, and right)
            *
            * @param objJSX {jsx3.gui.Dialog} the dialog to be resized
            */
            commondialog.resizeDialog = function(objJSX) {
                if (objJSX == null) return;
                var parent = objJSX.getParent();
                pArr = parent.getAbsolutePosition(parent.getParent());
                var dArr = objJSX.getDimensions();
                dArr[0] = 10;
                dArr[2] = pArr.W - 20;
                dArr[1] = 10;
                dArr[3] = pArr.H - 20;
                objJSX.setDimensions(dArr, true);
            };

            /**
            * Given a dialog, makes sure that any given edge of the dialog is positioned within the bounds of it's parent
            * by at least 10 pixels.
            *
            * @param objJSX {jsx3.gui.Dialog} the dialog to resize relative to the parent, if necessary
            */
            commondialog.resizeDialogIfLarger = function(objJSX) {
                if (objJSX == null) return;
                var parent = objJSX.getParent();
                pArr = parent.getAbsolutePosition(parent.getParent());
                var dOrig = objJSX.getAbsolutePosition(parent);
                var dArr = new Object();
                dArr.L = dOrig.L - pArr.L;
                dArr.T = dOrig.T - pArr.T;
                dArr.W = dOrig.W;
                dArr.H = dOrig.H;
                var changed = false;
                if (dArr.L < 10) {
                    //                    ux.core.util.Log.RESOURCES.warn("moving right");
                    dArr.L = 10;
                    changed = true;
                }
                if (dArr.T < 10) {
                    //                    ux.core.util.Log.RESOURCES.warn("moving down");
                    dArr.T = 10;
                    changed = true;
                }
                if (dArr.W > (pArr.W - 20)) {
                    //                    ux.core.util.Log.RESOURCES.warn("sizing width");
                    dArr.L = 10;
                    dArr.W = pArr.W - 20;
                    changed = true;
                }
                if (dArr.H > (pArr.H - 20)) {
                    //                    ux.core.util.Log.RESOURCES.warn("sizing height");
                    dArr.T = 10;
                    dArr.H = pArr.H - 20;
                    changed = true;
                }
                if (changed) {
                    //                    ux.core.util.Log.RESOURCES.warn("applying size: " + dArr.L + "," + dArr.T + "," + dArr.W + "," + dArr.H);
                    objJSX.setDimensions(dArr.L, dArr.T, dArr.W, dArr.H, true);
                }
            };

            /**
            * Positions a given dialog as centered within it's parent (does not ensure that the dialog does not go outside
            * it's parent's bounds, so a successive call to <code>resizeDialogIfLarger</code> may be desired.
            *
            * @param objDialog {jsx3.gui.Dialog} the dialog to center
            */
            commondialog.centerDialog = function(objDialog) {
                var loc = objDialog.getParent().getAbsolutePosition();
                objDialog.setLeft((loc.W - objDialog.getWidth()) / 2, false);
                objDialog.setTop((loc.H - objDialog.getHeight()) / 4, false);
                objDialog.repaint();
            };

            commondialog.setCaptionBar = function(ctionbar, strDynPropName) {
                if (ctionbar && strDynPropName) {
                    ctionbar.setDynamicProperty("jsxtext", strDynPropName).repaint();
                }
            }


            commondialog.setContent = function(domNode, strURL) {
                var laodedcontent
                if (domNode && strURL) {
                    laodedcontent = ux.core.util.DynamicLoad.loadComponent(domNode, strURL, true);
                }
                return laodedcontent;
            }


            commondialog.destroyDialog = function(dialog) {
                dialog.backReference.dialog = null;
                dialog.doClose()
                dialog = null;

            }


        });